package com.whiteops.sdk;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.Base64;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.google.android.exoplayer2.source.chunk.ChunkedTrackBlacklistUtil;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONException;
import org.json.JSONObject;

final class d {
    WebView a = null;
    String b;
    CookieManager c = CookieManager.getInstance();
    Timer d;
    boolean e = false;
    boolean f = false;

    public class a {
        public a() {
        }

        @JavascriptInterface
        public final String getJSONData() {
            StringBuilder sb;
            String str = "---------getJSONData----->" + d.this.b;
            if (str.length() > 3900) {
                str = "<>" + str;
                while (str.length() > 3900) {
                    str.substring(0, 3900);
                    str = str.substring(3900);
                }
                sb = new StringBuilder();
            } else {
                sb = new StringBuilder("<>");
            }
            sb.append(str);
            sb.append("</>");
            return d.this.b != null ? d.this.b : "";
        }

        @JavascriptInterface
        public final void make_request(String str, String str2, String str3) {
            WOInspector.a(str, str2, str3);
        }

        @JavascriptInterface
        public final String ready(String str) {
            if (d.this.e) {
                return "B1.0.5.01";
            }
            d.this.e = true;
            if (d.this.d != null) {
                d.this.d.cancel();
                d.this.d.purge();
            }
            if (str.toLowerCase().contains("beta_kill")) {
                String string = WOInspector.e.getSharedPreferences("WOInspector", 0).getString("wossid", "");
                WOInspector.f = string;
                if (!string.equals("")) {
                    byte[] decode = Base64.decode(WOInspector.f, 0);
                    byte[] bytes = "wossid".getBytes();
                    byte[] bArr = new byte[decode.length];
                    for (int i = 0; i < decode.length; i++) {
                        bArr[i] = (byte) (decode[i] ^ bytes[i % bytes.length]);
                    }
                    WOInspector.f = new String(bArr);
                }
                JSONObject jSONObject = new JSONObject();
                JSONObject jSONObject2 = new JSONObject();
                try {
                    jSONObject2.put("TYPE", "KILL");
                    jSONObject2.put("PROXY_ORIGIN", WOInspector.d);
                    jSONObject.put("CI", WOInspector.a);
                    jSONObject.put("ID", WOInspector.f);
                    jSONObject.put("DECISION_EVENT", jSONObject2);
                    jSONObject.put("CT", WOInspector.b);
                } catch (JSONException unused) {
                }
                JSONObject jSONObject3 = new JSONObject();
                try {
                    Iterator<String> keys = jSONObject.keys();
                    while (keys.hasNext()) {
                        String next = keys.next();
                        jSONObject3.put(next, jSONObject.get(next));
                    }
                    jSONObject3.put("DATA", "");
                    WOInspector.g.a(jSONObject3.toString());
                    return "B1.0.5.01";
                } catch (JSONException unused2) {
                    return "B1.0.5.01";
                }
            } else {
                WOInspector.a();
                return "B1.0.5.01";
            }
        }

        @JavascriptInterface
        public final void set_conf(String str) {
        }
    }

    @SuppressLint({"NewApi", "SetJavaScriptEnabled"})
    d() {
        this.c.setAcceptCookie(false);
        CookieManager.setAcceptFileSchemeCookies(false);
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @SuppressLint({"AddJavascriptInterface"})
            public final void run() {
                try {
                    d.this.a = new WebView(WOInspector.e);
                    if (d.this.a instanceof WebView) {
                        d.this.a.getSettings().setJavaScriptEnabled(true);
                        d.this.a.setWebViewClient(new b());
                        d.this.a.addJavascriptInterface(new a(), "ozoki_nt");
                        d.this.c.setAcceptThirdPartyCookies(d.this.a, false);
                        return;
                    }
                    d.this.f = true;
                    d.this.a = null;
                } catch (Exception e) {
                    d.this.f = true;
                    d.this.a = null;
                    new StringBuilder("------##--------------->FAIL: webview creation throw exception: ").append(e.getMessage());
                }
            }
        });
    }

    /* access modifiers changed from: package-private */
    @SuppressLint({"SimpleDateFormat"})
    public final void a() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            public final void run() {
                if (d.this.a != null) {
                    try {
                        String format = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
                        d.this.a.loadUrl(WOInspector.c + "?ci=" + WOInspector.a + "&cb=" + format + "&sdk=b");
                    } catch (Exception e) {
                        d.this.f = true;
                        new StringBuilder("--------------------->FAIL initHTML. Throw exception: ").append(e.getMessage());
                    }
                } else {
                    d.this.f = true;
                }
            }
        });
        b();
    }

    /* access modifiers changed from: package-private */
    public final void a(String str) {
        this.b = str;
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            public final void run() {
                if (d.this.a != null) {
                    try {
                        d.this.a.loadUrl("javascript:ozoki_zp.load(ozoki_nt.getJSONData())");
                    } catch (Exception e) {
                        new StringBuilder("------##---------------> WebView sendData Throw Exception: ").append(e.getMessage());
                    }
                }
            }
        });
    }

    /* access modifiers changed from: package-private */
    public final void b() {
        if (!this.f) {
            this.d = new Timer();
            this.d.schedule(new TimerTask() {
                public final void run() {
                    if (!d.this.e) {
                        String str = "";
                        try {
                            str = InetAddress.getByName(Uri.parse(WOInspector.c).getHost()).getHostAddress();
                        } catch (UnknownHostException unused) {
                        }
                        if (!str.equals("")) {
                            d.this.a();
                        } else {
                            d.this.b();
                        }
                    }
                }
            }, ChunkedTrackBlacklistUtil.DEFAULT_TRACK_BLACKLIST_MS);
        }
    }
}
